//
//  AViewController.swift
//  UIDemo
//
//  Created by MAC on 2016/12/29.
//  Copyright © 2016年 MAC. All rights reserved.
//

import UIKit

class AViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var baby = ["北鼻0","北鼻1","北鼻2","北鼻3","北鼻4","北鼻5","北鼻6","北鼻7","北鼻8","北鼻9","北鼻10","北鼻11"]
    
    var babyImg = ["baby0.jpg","baby1.jpg","baby2.jpg","baby3.jpg","baby4.jpg","baby5.jpg","baby6.jpg","baby7.jpg","baby8.jpg","baby9.jpg","baby10.jpg","baby11.jpg"]
    
    lazy var _tableViews = UITableView()
    
    var isFlag = [Bool](repeating:false,count:12)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        _tableViews.frame = CGRect(origin:CGPoint(x:0,y:0),size:CGSize(width:self.view.frame.size.width,height:self.view.frame.size.height))
        _tableViews.delegate = self
        _tableViews.dataSource = self
        
        self.view.addSubview(_tableViews)
        
        // Do any additional setup after loading the view.
    }
    
    /************************ 表的代理方法 ************************/
    //实现表的代理方法
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return baby.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let initIdentifier = "cell"
        let cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: initIdentifier)
        
        //textLabel赋值
        cell.textLabel?.text = baby[indexPath.row]
        //detailTextLabel赋值
        cell.detailTextLabel?.text = "baby\(indexPath.row)"
        
        cell.imageView?.image = UIImage(named:babyImg[indexPath.row])
        cell.imageView?.layer.cornerRadius = 40
        cell.imageView?.layer.masksToBounds = true
        
        cell.accessoryType = UITableViewCellAccessoryType.detailButton
        if isFlag[indexPath.row] {
            cell.accessoryType = UITableViewCellAccessoryType.checkmark
        }else{
            cell.accessoryType = UITableViewCellAccessoryType.none
        }
        
        return cell
    }
    //表触发方法
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let locationActionHandler = {(action:UIAlertAction!) -> Void in
            let locationAlertController = UIAlertController(title:nil,message:"我爱baby\(indexPath.row)",preferredStyle:UIAlertControllerStyle.alert)
            let okAction = UIAlertAction(title:"OK",style:UIAlertActionStyle.default,handler:nil)
            
            locationAlertController.addAction(okAction)
        self.present(locationAlertController,animated:true,completion:nil)
            
        }
        
        let alertController = UIAlertController(title:"baby\(indexPath.row)",message:nil,preferredStyle:UIAlertControllerStyle.actionSheet)
        
        let cancleAction = UIAlertAction(title:"Cancle",style:UIAlertActionStyle.cancel,handler:nil)

        alertController.addAction(cancleAction)
        
        let locationAction = UIAlertAction(title:"baby是几号",style:UIAlertActionStyle.default,handler:locationActionHandler)
        
        alertController.addAction(locationAction)
        
        let markAction = UIAlertAction(title:"标记baby一下",style:UIAlertActionStyle.default,handler:{(action:UIAlertAction)-> Void in
            let cell = tableView.cellForRow(at: indexPath)
            
            cell?.accessoryType = UITableViewCellAccessoryType.checkmark
            
            self.isFlag[indexPath.row] = true
            
        })
        alertController.addAction(markAction)
        present(alertController, animated: true, completion: nil)
        
    }
    
    //和下一个方法中实现deleteAction效果是一样的
    //  func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    //        if editingStyle == UITableViewCellEditingStyle.Delete{
    //        self.baby.removeAtIndex(indexPath.row)
    //        self.babyImage.removeAtIndex(indexPath.row)
    //        self.isFlag.removeAtIndex(indexPath.row)
    //        self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Middle)
    //       }  
    //    }
    
    //分享和删除功能的实现
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        
        let edit = UITableViewRowAction(style: .normal, title: "分享") { action, index in
            // OPEN UIPresentationController HERE
            //.present(vc, animated: true, completion: nil)
            //创建提示框
            let menu = UIAlertController(title:nil ,message:nil,preferredStyle:UIAlertControllerStyle.alert)
            
            //创建退出按钮
            let cancelAction = UIAlertAction(title:"退出",style:UIAlertActionStyle.cancel,handler:nil)
            
            //创建分享平台按钮
            let facebookAction = UIAlertAction(title:"FaceBook",style:UIAlertActionStyle.default,handler:nil)
            //创建分享平台按钮
            let twitterAction = UIAlertAction(title:"Twitter",style:UIAlertActionStyle.default,handler:nil)
            
            
            //添加到提示框中
            menu.addAction(facebookAction)
            menu.addAction(twitterAction)
            menu.addAction(cancelAction)
            
            
            self.present(menu, animated: true, completion: nil);
        }
        let delega = UITableViewRowAction(style: .default, title: "删除") { action, index in
            // OPEN UIPresentationController HERE
            //.present(vc, animated: true, completion: nil)
            print("------- 删除")
        }
        
        
        
        return [edit,delega]
    }
    
    //设置cell高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 120
    }
    
    /************************ 表的代理方法 ************************/
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
